"""
    @Author  ：思念 
    @File    ：11.唯品会商品数据抓取.py
    @Date    ：2025/4/8 11:29 
"""

import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from random import randint
from pymongo import MongoClient


class VipGoods:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.mongo_client = MongoClient()
        self.db = self.mongo_client['py_spider']['vip_goods']
        self.cookies = {
            'vip_first_visitor': '1',
            'vip_address': '%257B%2522pid%2522%253A%2522105101%2522%252C%2522cid%2522%253A%2522105101101%2522%252C%2522pname%2522%253A%2522%255Cu56db%255Cu5ddd%255Cu7701%2522%252C%2522cname%2522%253A%2522%255Cu6210%255Cu90fd%255Cu5e02%2522%257D',
            'vip_province': '105101',
            'vip_province_name': '%E5%9B%9B%E5%B7%9D%E7%9C%81',
            'vip_city_name': '%E6%88%90%E9%83%BD%E5%B8%82',
            'vip_city_code': '105101101',
            'vip_wh': 'VIP_CD',
            'vip_ipver': '31',
            'mars_cid': '1744082959616_51aa36d96ad674f348d263a9dbd636fd',
            'mars_sid': 'ef7096d1f48a023499bb374d41033675',
            'pc_fdc_area_id': '105101101',
            'pc_fdc_source_ip': '1',
            'is_default_area': '1',
            'mars_pid': '0',
            'VIP_QR_FIRST': '1',
            'visit_id': '3767A56BE6CD2DD6A36C8627F224981D',
            'vipshop_passport_src': 'https%3A%2F%2Fwww.vip.com%2F',
            '_jzqco': '%7C%7C%7C%7C%7C1.1131509916.1744094000362.1744094000362.1744094000362.1744094000362.1744094000362.0.0.0.1.1',
            'PASSPORT_ACCESS_TOKEN': '96F90AC01AD533F380670CBFFAD60C1711286943',
            'VipRUID': '477087290',
            'VipUID': '6c3af59bff6101c0da03febe56bc8d3e',
            'VipRNAME': 'ph_*****************************25e',
            'VipLID': '0%7C1744094038%7Cae1d82',
            'VipDegree': 'D1',
            'user_class': 'b',
            'VipUINFO': 'luc%3Ab%7Csuc%3Ab%7Cbct%3Ac_new%7Chct%3Ac_new%7Cbdts%3A0%7Cbcts%3A0%7Ckfts%3A0%7Cc10%3A0%7Crcabt%3A0%7Cp2%3A0%7Cp3%3A1%7Cp4%3A0%7Cp5%3A1%7Cul%3A3105',
            'fe_global_sync': '1',
            'sfl_d': '0',
            'smidV2': '20250408144117c56ac18bc05e346997a682da28afc637002a4b5a526899570',
            '.thumbcache_f65dad1092aa9e66c73b4823b4493a2f': 'fUk4PqaiDCmDtGYSgzj9wMaiiDqR9R+akozq9OjF+hp3FSGI9Icx2ZBr4RB5c99H3JDX1z2blVMvTJ+5e0ALxg%3D%3D',
            'tfs_fp_token': 'BfUk4PqaiDCmDtGYSgzj9wMaiiDqR9R+akozq9OjF+hp3FSGI9Icx2ZBr4RB5c99H3JDX1z2blVMvTJ+5e0ALxg%3D%3D',
            'tfs_fp_timestamp': '1744094477439',
            'vip_tracker_source_from': '',
            'pg_session_no': '9',
            'waitlist': '%7B%22pollingId%22%3A%22CE311271-10D9-4703-AE3E-D67A17C5CE79%22%2C%22pollingStamp%22%3A1744094592074%7D',
        }

    # 获取商品页面
    def get_goods_page(self):
        self.driver.get('https://category.vip.com/suggest.php?keyword=%E6%B1%89%E6%9C%8D&page={}')
        for key, value in self.cookies.items():
            cookie = {
                'name': key,
                'value': value
            }
            self.driver.add_cookie(cookie)
        self.driver.refresh()
        time.sleep(randint(1, 2))

    # 滚动到底部
    def dropdown_menu(self):
        for i in range(1, 10):
            js_code = f'window.scrollBy(0, {i * 1000})'
            self.driver.execute_script(js_code)
            time.sleep(randint(1, 2))

    def get_goods_info(self):
        # 获取数据之前，需滑动到底部
        self.dropdown_menu()
        div_list = self.driver.find_elements(
            By.XPATH,
            '//section[@id="J_searchCatList"]/div[@class="c-goods-item  J-goods-item c-goods-item--auto-width"]'
        )
        for div in div_list:
            price = div.find_element(
                By.XPATH,
                './/div[@class="c-goods-item__sale-price J-goods-item__sale-price"]'
            ).text

            title = div.find_element(
                By.XPATH,
                './/div[2]/div[2]'
            ).text

            dict = {
                'title': title,
                'price': price
            }
            self.save_to_mongo(dict)
        self.next_page()

    # 数据保存
    def save_to_mongo(self, dict):
        if dict:
            self.db.insert_one(dict)
            print('保存成功:', dict)
        else:
            print('保存失败:', dict)
    def next_page(self):
        try:
            next_button = self.driver.find_element(By.XPATH, '//*[@id="J_nextPage_link"]')
            if next_button:
                next_button.click()
                self.get_goods_info()  # 获取下一页数据
            else:
                print('没有下一页...')
                self.driver.quit()
        except NoSuchElementException:
            print('未获取到元素')
            self.driver.quit()
            self.mongo_client.close()

    def main(self):
        self.get_goods_page()
        self.get_goods_info()


if __name__ == '__main__':
    vip_goods = VipGoods()
    vip_goods.main()
